<?php

require_once 'i_navigation.php';
/**
 * The super class for all web pages.
 * Public variables required:
 * 
 */
class Page {
	/** Show breadcrumb at top of top. */
	public $showBreadcrumb = true;
	/** Show top - top is above left/main/right and under breadcrumb */
	public $showTop = false;
	/** Show content header - header is above main/right and is at the same level with left */
	public $showContentHeader = false;
	/** Show right panel - default false*/
	public $showRight = false;
	/** Show left nav panel */
	public $showLeft = true;
	
	/** Width of the left column, '-1' means using value declared in athena.css, which is 195px */
	public $widthLeft = -1; 
	/** Width of the right column, '-1' means using value declared in athena.css, which is 195px */
	public $widthRight = -1;

	/** Extra style information for div#top, ending with ';' */
	public $styleTop = "";	
	/** Extra style information for div#left, ending with ';' */
	public $styleLeft = "";
	/** Extra style information for div#right, ending with ';' */
	public $styleRight = "";
	/** Extra style information for div#content_header, ending with ';' */
	public $styleContentHeader = "";
	/** Extra style information for div#main, ending with ';' */
	public $styleBodyMain = "";
	
	/** Array of selected menu path from root to leaf */
	public $menuSelectedChain = null;
	/** Menu item corresponding to current page or null if does not exist 
	 * @var MenuItem */
	public $menuItem = null;
	/** The selected category - one level down to the $MENU_ROOT */
	public $menuSelectedCategory = null;
	
	/** The page title - when displayed, the title will always be appended with '- Athena Framework' */
	public $title = null;
	/** Specify for your custom CSS or leave it null to use the default */
	public $cssCore = null;
	
	/** Output all the page code */
	public function output() {
		$this->init();
		$this->pageHeader();
		$this->contentOpen();
		if($this->showTop) {
			$this->topOpen();
			$this->top();
			$this->topClose();
		}
		
		$this->rightmainleftwrapperOpen();
		if($this->showContentHeader) {
			$this->contentHeaderOpen();
			$this->contentHeader();
			$this->contentHeaderClose();
		}
		if($this->showRight) {
			$this->rightOpen();
			$this->right();
			$this->rightClose();
		}
		$this->mainOpen();
		$this->main();
		$this->mainClose();
		$this->rightmainleftwrapperClose();
		if($this->showLeft) {
			$this->leftOpen();
			$this->left();
			$this->leftClose();
		}
		
		$this->contentClose();
		$this->pageFooter();
	}
	
	/** Called before printing out any HTML code. */
	public function init() {
		global $MENU_ROOT;
		// calculate selected menu chain
		$this->menuSelectedChain = $MENU_ROOT->searchPath($this->getPageUrl());
		$this->menuSelectedCategory = 
			(is_array($this->menuSelectedChain) && count($this->menuSelectedChain) >= 2) ?
			$this->menuSelectedChain[1] : null;
		if($this->menuSelectedChain == NULL) {
			$this->menuSelectedChain = array($MENU_ROOT); // always display the top nav bar.
		}else{
			$this->menuItem = $this->menuSelectedChain[count($this->menuSelectedChain) - 1];
		}
	}
	
	/** Returns the calculated page URL (with sorted non-system GET params) 
	 * Subclass may override this method to provide custom URL. 
	 * @return string
	 * */ 
	public function getPageUrl() {		
		return getURLForThisPage();
	}


	// --------------------------------------- Content  ---------------------------------------
	/** Prints out content opening code - 
	 * container on enter: div#bodycontainer 
	 * container on leave: div#bodycontainer/div#contentcontainer/div#content 
	 */
	public function contentOpen() {
		print "<div id=\"contentcontainer\"><div id=\"content\">";
	}
	
	/** Prints out content closing code - 
	 * container on enter: div#bodycontainer/div#contentcontainer/div#content
	 * container on leave: div#bodycontainer */
	public function contentClose() {
		print("</div></div> <!-- end content -->\n");
	}	
	
	// --------------------------------------- Top  ---------------------------------------
	/** Prints out top opening code - 
	 * container on enter: div#bodycontainer/div#contentcontainer/div#content 
	 * container on leave: div#bodycontainer/div#contentcontainer/div#content/div#top
	 */
	public function topOpen() {
		print "<div id=\"top\">";
	}
	
	/** 
	 * Prints on the top area. By default, breadcrumb is printed if showBreadcrumb is true.
	 */
	public function top() {
		global $LANGUAGE;
		if($this->showBreadcrumb) {
			$breadcrumbCode = $this->printBreadCrumb(); 
			print("<div id=\"breadcrumb\" class=\"$LANGUAGE\">$breadcrumbCode</div>\n");
		}
	}
	
	/** Prints out top closing code - 
	 * container on enter: div#bodycontainer/div#contentcontainer/div#content/div#top
	 * container on leave: div#bodycontainer/div#contentcontainer/div#content 
	 */
	public function topClose() {
		print("</div> <!-- end top -->\n");
	}	
	
	// --------------------------------------- Right, main left wrapper  ---------------------------------------
	/** Prints out right main left wrapper opening code - 
	 * container on enter: div#bodycontainer/div#contentcontainer/div#content 
	 * container on leave: div#bodycontainer/div#contentcontainer/div#content/div#wrapper_right_main_outer_left_on/div#wrapper_right_main_inner_left_on
	 */
	public function rightmainleftwrapperOpen() {
		if($this->showLeft) {
			print "<div id=\"wrapper_right_main_outer_left_on\"><div id=\"wrapper_right_main_inner_left_on\">
				<!-- start right main left wrapper -->\n";
		}else{
			print "<div class=clearing />";
		}
	}

	/** Prints out right main left wrapper opening code - 
	 * container on enter: div#bodycontainer/div#contentcontainer/div#content/div#wrapper_right_main_outer_left_on/div#wrapper_right_main_inner_left_on
	 * container on leave: div#bodycontainer/div#contentcontainer/div#content
	 */
	public function rightmainleftwrapperClose() {
		if($this->showLeft) {
			print("</div></div> <!-- end right main left wrapper -->\n");
		}
	}	

	// --------------------------------------- Content header ---------------------------------------
	/** Prints out content header opening code 
	 * Enter: div#bodycontainer/div#contentcontainer/div#content/WRAPPER/
	 * Leave: div#bodycontainer/div#contentcontainer/div#content/WRAPPER/div#content_header
	 * 
	 * */
	public function contentHeaderOpen() {
		global $LANGUAGE;
		print("<div id=\"content_header\"> <!-- content header start -->\n");
	}
	
	/** Prints out content header code - subclass may override this function to add code to content header. */
	public function contentHeader() {
		print("[CONTENT HEADER]");
	}
	
	/** Prints out content header closing code 
	 * Enter: div#bodycontainer/div#contentcontainer/div#content/WRAPPER/div#content_header
	 * Leave: div#bodycontainer/div#contentcontainer/div#content/WRAPPER/
	 * */
	public function contentHeaderClose() {
		print("</div> <!-- END: content_header -->\n");
	}
	
	// --------------------------------------- main body ---------------------------------------
	/** Prints out content body opening code 
	 *  Enter: div#bodycontainer/div#contentcontainer/div#content/WRAPPER/
	 *  Leave: div#bodycontainer/div#contentcontainer/div#content/WRAPPER/div#main
	 * */
	public function mainOpen() {
		// clear the 'margin-right' for main when right is off.
		print("<div id=\"main\" style=\"$this->styleBodyMain\"> <!-- main body start -->\n");
	}

	/** Prints out content body code - subclass may override this function to add code to content body. */
	public function main() {
		print("[Main]");
	}

	/** Prints out content body closing code 
	 *  Enter: div#bodycontainer/div#contentcontainer/div#content/WRAPPER/div#main
	 *  Leave: div#bodycontainer/div#contentcontainer/div#content/WRAPPER/
	 */
	public function mainClose() {
		print("</div> <!-- main end -->\n");
	}	
	
	// --------------------------------------- Right ---------------------------------------
	/** Prints out right panel opening code 
	 *  Enter: div#bodycontainer/div#contentcontainer/div#content/WRAPPER/
	 *  Leave: div#bodycontainer/div#contentcontainer/div#content/WRAPPER/div#right
	 * */
	public function rightOpen() {
		print("<div id=\"right\" style=\"$this->styleRight\"> <!-- right start -->\n");
	}

	/** Prints out contact panel - subclass may override this function to add code to right panel. */
	public function right() {
		print("[Right]");
	}
	
	/** Prints out right panel closing code 
	 *  Enter: div#bodycontainer/div#contentcontainer/div#content/WRAPPER/div#right
	 *  Leave: div#bodycontainer/div#contentcontainer/div#content/WRAPPER/
	 * */
	public function rightClose() {
		print("</div> <!-- right end -->\n");
	}		
	
	// --------------------------------------- Left ---------------------------------------
	/** Prints out left nav panel opening code 
	 *  Enter: div#bodycontainer/div#contentcontainer/div#content/WRAPPER/
	 *  Leave: div#bodycontainer/div#contentcontainer/div#content/WRAPPER/div#left
	 * */
	public function leftOpen() {
		print("<div id=\"left\" style=\"$this->styleLeft\"> <!-- left start -->\n");
	}

	/** Prints out left nav panel code - subclass may override this function to add code to left nav panel. */
	public function left() {
		global $MENU_ROOT, $LANGUAGE, $COUNTRY, $ROLES;
		print("<div id=\"navpanel\" style=\"padding: 6px;\">"); // navigation panel 
		if($this->menuSelectedCategory != null) {
			print("<div class=\"nav_rcb\" style=\"width: 100% \"> <!-- nav panel starts -->\n");
			print("<h2 class=\"header nav_title\">" . $this->menuSelectedCategory->getLabel($LANGUAGE) . "</h2>\n");
			print($this->menuSelectedCategory->toHtmlMenu($this->menuSelectedChain, $LANGUAGE, $COUNTRY, $ROLES, false));
			print("</div> <!-- nav panel end -->\n");
		}
	}

	/** Prints out left nav panel closing code 
	 *  Enter: div#bodycontainer/div#contentcontainer/div#content/WRAPPER/div#left
	 *  Leave: div#bodycontainer/div#contentcontainer/div#content/WRAPPER/
	 * */
	public function leftClose() {
		print("</div><! -- left end -->");
	}
	
	// --------------------------------------- Page header and page footer ---------------------------------------
	
	/**
	 * Renders the level 1 menu items using images instead of default text.
	 * @param $menuItem
	 * @return unknown_type
	 */
	function menuItemRenderLevel1AsImage(MenuItem $menuItem, $selected, $language, $country) {
		global $URL_ROOT_WITH_CC_LG, $URL_DIR_IMG;
		$lang2Letter = substr($language, 0, 2);
		if($menuItem->getLevel() == 1) {
			return "<a href=\"$URL_ROOT_WITH_CC_LG" . $menuItem->getPath() . "\">" . 
				"<img src=\"{$URL_DIR_IMG}/header/menu/$menuItem->id" . "_$lang2Letter.png\">" .
				"</a>";
		}else{
			return "<a href=\"$URL_ROOT_WITH_CC_LG" . $menuItem->getPath() . "\">" . $menuItem->getLabel($language) . "</a>";
		}
	}
	
	/** Used to insert additional elements, e.g., CSS/JS in <head></head>. */
	public function pageAdditionalElementsInHead() {
		
	}
	
	/** Page header 
	 * Leave: div#bodycontainer
	 * */
	public function pageHeader() {
		global $MENU_ROOT, $LANGUAGE, $COUNTRY, $ROLES, $URL_ROOT, $URL_ROOT_WITH_CC_LG, $URL_DIR_CSS, $URL_DIR_JS, $URL_DIR_IMG;
		$menuCode = $MENU_ROOT->toHtmlMenu($this->menuSelectedChain, $LANGUAGE, $COUNTRY, $ROLES, false, 1, false);
		$fulltitle = $this->title ? $this->title . " - Athena Framework" : "X ERROR: Page.title not set";
		$thecss = $this->cssCore ? $this->cssCore : $URL_DIR_CSS . "athena.css";
		// div 'all' must have 'position: relative;' style, otherwise IE 7 will display incorrectly.
		print <<< PAGE_HREADER_OPEN
			<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
			<html xmlns="http://www.w3.org/1999/xhtml">
			<head>
			<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
			<title>$fulltitle</title>
			<link rel="stylesheet" type="text/css" href="$thecss" media="all"/>
			<script type="text/javascript" src="{$URL_DIR_JS}jquery-1.4.2.min.js"></script>
			<script type="text/javascript" src="{$URL_DIR_JS}insprise_roundbox.js"></script>
			<script type="text/javascript" src="{$URL_DIR_JS}athena_core.js"></script>
PAGE_HREADER_OPEN;

if(is_array($this->requiredComponents)) {
	for($c = 0; $c < count($this->requiredComponents); $c++) {
		echo self::$COMPONENTS[$this->requiredComponents[$c]];
		echo "\n";
	}
}
		
$this->pageAdditionalElementsInHead();
print <<< PAGE_HREADER_CLOSE
			</head>
			
			<body>
			<div id="bodycontainer">
			
			<div id="headercontainer">
				<div id="header">
					<div id="logo"><a href='$URL_ROOT'><img alt="Athena Framework Homepage" src="${URL_DIR_IMG}logo/logo.png"></a></div>
					<div id="quicklinks"></div>
					<div id="navbar">
						$menuCode
					</div>
				</div> <!-- end header -->
			</div> <!-- end headercontainer -->
PAGE_HREADER_CLOSE;
	}
	
	/** Page footer 
	 * Enter: div#bodycontainer
	 * */
	public function pageFooter() {
		global $URL_ROOT_WITH_CC_LG, $SERVER_ID, $DEBUG;
		date_default_timezone_set('UTC');
		$year = date('Y');
		print <<< PAGE_FOOTER_A
			<div class="clearing" style=""/>
			<div id="footercontainer" style="padding-bottom: 12px;">
			<div id="footer" style="font-size: 11px;">

			<hr style="color: #f8f8f8; background-color: #f8f8f8;"/>
			<div id="footerlinks">
			<a href="{$URL_ROOT_WITH_CC_LG}aboutus/privacy.php" target=_blank style='color: #666'>Privacy Policy</a> |
			<a href="{$URL_ROOT_WITH_CC_LG}aboutus/legal.php" target=_blank style='color: #666'>Legal Notice &amp; Term of Use </a> |
			<a href="mailto:info@athenasource.org" target=_blank style='color: #666'>Feedback &amp; Enquiry</a>
			<br />
			<span class="copyright">© $year All Rights Reserved by AthenaSource</span>
			</div> <!-- div footerlinks -->
			
			
			</div> <!-- div footer -->
			</div> <!-- footercontainer -->
			
			</div><!-- div id "bodycontainer" -->
PAGE_FOOTER_A;

if(!$DEBUG) {
print <<< PAGE_FOOTER_B
<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-22064820-1']);
  _gaq.push(['_trackPageview']);
  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
</script>
PAGE_FOOTER_B;
}

print "</body></html>";
		// print "URL: " . $this->getPageUrl();
		// print "<br>Selected menu path: " . $this->printArray($this->menuSelectedChain);
	}
	
	// --------------------------------------- required component/plugin files -------------------------------
	// Component/plugins required js and css will be placed in HEAD (seee pageHeader()).
	/**
	 * components/plugins that can be used on this page, mapping of name to required declarations in HEAD.
	 * @var Array
	 */
	static $COMPONENTS;
	
	static function initComponents() {
		global $URL_DIR_JS, $URL_DIR_IMG;
		if(is_array(self::$COMPONENTS)) {
			return;
		}
		self::$COMPONENTS = array();
		self::$COMPONENTS["niceform"] = "<script type=\"text/javascript\" src=\"${URL_DIR_JS}niceform/niceforms.js\"></script>\n<link rel=\"stylesheet\" type=\"text/css\" href=\"${URL_DIR_JS}niceform/niceforms-default.css\" media=\"all\"/>";
		self::$COMPONENTS["syntaxhighlighter"] = "<script type='text/javascript' src='${URL_DIR_JS}syntaxhighlighter/scripts/shCore.js'></script>
			<script type='text/javascript' src='${URL_DIR_JS}syntaxhighlighter/scripts/shBrushJava.js'></script>
			<script type='text/javascript' src='${URL_DIR_JS}syntaxhighlighter/scripts/shBrushAS3.js'></script>
			<script type='text/javascript' src='${URL_DIR_JS}syntaxhighlighter/scripts/shBrushXml.js'></script>
			<script type='text/javascript' src='${URL_DIR_JS}syntaxhighlighter/scripts/shBrushSql.js'></script>
			<link type='text/css' rel='stylesheet' href='${URL_DIR_JS}syntaxhighlighter/styles/shCoreDefault.css'/>
			<script type='text/javascript'>SyntaxHighlighter.defaults['gutter'] = false; SyntaxHighlighter.defaults['toolbar'] = false; 
			SyntaxHighlighter.defaults['tab-size'] = 2; SyntaxHighlighter.defaults['auto-links'] = false; SyntaxHighlighter.all();</script>";
		self::$COMPONENTS["fancybox"] = "<script type='text/javascript' src='${URL_DIR_JS}fancybox/jquery.fancybox-1.3.1.pack.js'></script>
			<link rel='stylesheet' href='${URL_DIR_JS}fancybox/jquery.fancybox-1.3.1.css' type='text/css' media='screen' />
			<script type='text/javascript'>		
			$(document).ready(function() {
				/* This is basic - uses default settings */
				if($(\"a.screenshot\").length > 0) {
					$(\"a.screenshot\").fancybox({
						'overlayOpacity'	:	0.5, 
						'overlayColor' : '#000',
						'hideOnContentClick' : 'true'
					});
				}	
			});
			jQuery(function($) {
			    $(\"a.screenshot img\").each(function() {
			        var iWidth = $(this).width();
			        var iHeight = $(this).height();
			        var letimg = \"<img src='${URL_DIR_IMG}icon/shortcut-orange.png' style='position: absolute; bottom: 8px; right: 18px; padding: 0px; border: 0px;' />\";
			        var wrapper;
			        if($(this).attr('align') == 'left') {
				        wrapper = $('<span style=\"position: relative; display: inline-block; float: left;\"></span>');
				    }else if($(this).attr('align') == 'right') {
					    wrapper = $('<span style=\"position: relative; display: inline-block; float: right;\"></span>');
				    }else{
				    	wrapper = $('<span style=\"position: relative; display: inline-block; \"></span>');
				    }
			        $(this).wrap(wrapper).after(letimg);
			    });
			});
		</script>";
	}
	
	/**
	 * Required components for this page, array elements are names of components.
	 */
	var $requiredComponents;
	
	/** Forms decoration library. */
	function requireComponent($compName) {
		self::initComponents();
		if(is_array($this->requiredComponents) && isset($this->requiredComponents[$compName])) {
			return;
		}
		if(! is_array($this->requiredComponents)) {
			$this->requiredComponents = array();
		}
		$this->requiredComponents[] = $compName;
	}
	
	/** Forms decoration library. */
	function requireNiceForms() {
		$this->requireComponent("niceform");
	}
	
	function requireSyntaxHighlighter() {
		$this->requireComponent("syntaxhighlighter");
	}
	
	function requireFancyBox() {
		$this->requireComponent("fancybox");
	}
	
	// --------------------------------------- Utility functions ---------------------------------------
	
	function printArray($arr) {
		$s = "{";
		$s .= '[' . count($arr) . ']';
		reset($arr);
		foreach($arr as $key => $val) {
			$s .= ($key . "=" . $val . ", ");
		}
		if(count($arr) > 0) {
			$s = substr($s, 0, strlen($s) - 2);
		}
		$s .= '}';
		return $s;
	}
	
	public function printBreadCrumb() {
		global $URL_ROOT, $LANGUAGE, $MENU_ROOT; 
		$selectChain = $this->menuSelectedChain;
		if(! is_array($selectChain)) {
			$selectChain = array($MENU_ROOT);
		}
		$s = "<span class=\"$LANGUAGE\">";
		for($i = 1; $i < count($selectChain); $i++) { // skip the root [0]
			if($i != 1) {
				$s .= '&nbsp;&nbsp;&gt;&nbsp;&nbsp;';
			}
			$s .= '<a href="' . $URL_ROOT . $selectChain[$i]->getPath() . '">' . $selectChain[$i]->getLabel($LANGUAGE) . '</a>';
		}
		$s .= "</span>";
		return $s;
	}
	

} // End class Page
?>